<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/100.gif" alt />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout" plain>退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- 侧边栏,宽度根据是否折叠进行设置 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 伸缩侧边栏按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 菜单 -->
        <el-menu
          :default-active="activePath"
          background-color="#333744"
          text-color="#fff"
          active-text-color="skyblue"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" :key="item.id" v-for="item in menuList">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconsObje[item.id]"></i> <span>{{item.authName}}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item @click="saveNavState('/'+subitem.path)" :index="'/'+subitem.path" :key="subitem.id" v-for="subitem in item.children">
              <template slot="title">
                <i class="el-icon-menu"></i> <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 图标列表 key值等于循环到的每项的id值
      iconsObje: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      // 左侧菜单是否折叠或展开显示
      isCollapse: false,
      // 被激活的动态显示的路由链接地址
      activePath: ''
    }
  },
  created() {
    // 在数据加载阶段请求菜单数据
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      // 清除token
      window.sessionStorage.clear()
      // 跳转到登陆页
      this.$router.push('/login')
    },
    async getMenuList() {
      const {data: res} = await this.$http.get('menus')
      this.menuList = res.data
      // console.log(res.data)
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(path) {
      // 点击二级菜单的时候保存被点击的二级菜单信息
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  padding-left: 0px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
      color: #fff;
      font-size: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: 0px;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button{
  text-align: center;
  line-height: 24px;
  letter-spacing: 4px;
  font-size: 10px;
  color: #fff;
  background-color: #4a5064;
  cursor: pointer;
}
</style>
